package com.dk.controller;

import com.dk.pojo.TbUserScoreAccount;
import com.dk.pojo.TbUserScoreDetail;
import com.dk.pojo.TbUserScoreUse;
import com.dk.service.UserScoreAccountService;
import com.dk.service.UserScoreDetailService;
import com.dk.service.UserScoreUseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Deacription TODO
 * @Author 陆小军
 * @Date 2020/12/28 10:11
 * @Version 1.0
 **/
@Component
public class MyJob {
    private final Logger log = LoggerFactory.getLogger(MyJob.class);

    @Autowired
    private UserScoreAccountService accountService;
    @Autowired
    private UserScoreDetailService detailService;
    @Autowired
    private UserScoreUseService userService;
    //cron表达式 每隔20秒触发一次
//    @Scheduled(cron = "0 0 1 * * ?")
    @Scheduled(cron = "0/20 * * * * ?")
    public void clearExpireScore(){
        log.info("清除过期积分方法开始:");
        try {
            //所有用户的过期积分
            List<TbUserScoreUse> scoreUseList = userService.selectScoreByDate(new Date());
            System.out.println(scoreUseList);
            //查询这些过期积分的用户id
            List<String> userIdList = userService.selectUserIdByDate(new Date());
            System.out.println(userIdList);
            //根据用户id ,删除当前所有过期积分,计算当前过期积分的和,总积分减去过期积分, 明细表中增加当前过期积分的信息
            List<Long> idList = new ArrayList<>();
            for (String userId : userIdList) {
                //定义当前userId过期总积分,
                Integer scores = 0;
                for (int i = 0; i < scoreUseList.size(); i++) {
                    TbUserScoreUse scoreUse = scoreUseList.get(i);
                    if(userId.equals(scoreUse.getUserId())){
                        scores = scores + scoreUse.getScore();
                    }
                    idList.add(scoreUse.getId());
                }
                //修改总积分
                TbUserScoreAccount scoreAccount = accountService.selectScoreAccount(userId);
                scoreAccount.setUpdateTime(new Date());
                scoreAccount.setExpendScore(scoreAccount.getExpendScore() + scores);
                scoreAccount.setTotalScore(scoreAccount.getTotalScore() - scores);
                accountService.updateScoreAccount(scoreAccount);
                //添加积分详情表
                TbUserScoreDetail detail = new TbUserScoreDetail();
                detail.setRemark("积分过期");
                detail.setCreateTime(new Date());
                detail.setExpireTime(new Date());
                detail.setUserId(userId);
                detail.setScoreDetail(-scores);
                detailService.insertScoreDetail(detail);
            }
            //删除所有过期积分的信息
            userService.deleteBatch(idList);
        }catch (Exception e){
            log.error("清除过期积分方法失败, ", e);
        }
    }
}
